package com.atguigu.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.atguigu.config.AlipayConfig;
import com.atguigu.entity.PaymentInfo;
import com.atguigu.enums.PaymentStatus;
import com.atguigu.enums.PaymentType;
import com.atguigu.result.RetVal;
import com.atguigu.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付信息表 前端控制器
 * </p>
 *
 * @author hachimen
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/payment")
public class PaymentInfoController {
    @Autowired
    private PaymentInfoService paymentInfoService;
    //1.返回支付二维码页面信息
    @RequestMapping("/createQrCode/{orderId}")
    public String createQrCode(@PathVariable Long orderId) throws Exception {

        return paymentInfoService.createQrCode(orderId);
    }

    //2.完成异步回调
    @PostMapping("/async/notify")
    public String asyncNotify(@RequestParam Map<String,String> paramMap) throws Exception {
        //修改交易订单状态
        boolean signVerified = AlipaySignature.rsaCheckV1(paramMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        if(signVerified){
            //验证成功
            String tradeStatus = paramMap.get("trade_status");
            if("TRADE_SUCCESS".equals(tradeStatus)||"TRADE_FINISHED".equals(tradeStatus)){
                //交易成功
                String outTradeNo = paramMap.get("out_trade_no");
                //查询获取支付信息
                PaymentInfo paymentInfo = paymentInfoService.getPaymentInfoByOutTradeNo(outTradeNo);
                //判断此支付信息是否已经完成
                if(PaymentStatus.PAID.name().equals(paymentInfo.getPaymentStatus())||
                PaymentStatus.ClOSED.name().equals(paymentInfo.getPaymentStatus())){
                    return "failure";
                }
                paymentInfoService.updatePaymentInfo(paramMap);
                return "success";
            }
        }else{
            //验证失败
            return "failure";
        }
        return "failure";
    }

    //3.发起退款 http://127.0.0.1:8005/payment/refund/28
    @RequestMapping("/refund/{orderId}")
    public RetVal refund(@PathVariable Long orderId) throws Exception{
        boolean flag = paymentInfoService.refund(orderId);
        return RetVal.ok(flag);
    }

    //4.关闭交易接口 http://127.0.0.1:8005/payment/closeAlipayTrade/28
    @GetMapping("/closeAlipayTrade/{orderId}")
    public boolean closeAlipayTrade(@PathVariable Long orderId) throws Exception{
        return paymentInfoService.closeAlipayTrade(orderId);
    }

    //5.查询阿里内部订单信息 http://127.0.0.1:8005/payment/queryAlipayTrade/28
    @GetMapping("/queryAlipayTrade/{orderId}")
    public boolean queryAlipayTrade(@PathVariable Long orderId) throws Exception{
        return paymentInfoService.queryAlipayTrade(orderId);
    }

    //6.查询阿里内部订单信息 http://127.0.0.1:8005/payment/queryAlipayTrade/28
    @GetMapping("/queryAlipayTradeStatus/{orderId}")
    public RetVal queryAlipayTradeStatus(@PathVariable Long orderId) throws Exception{
        AlipayTradeQueryResponse alipayTradeQueryResponse = paymentInfoService.queryAlipayTradeStatus(orderId);
        if(alipayTradeQueryResponse.isSuccess()){
            return RetVal.ok(alipayTradeQueryResponse.getTradeStatus());
        }else{
            return RetVal.fail();
        }
    }

    //获取PaymentInfo
    @GetMapping("/getPaymentInfo/{outTradeNo}")
    public PaymentInfo getPaymentInfo(@PathVariable String outTradeNo){
        return paymentInfoService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
    }






}

